package com.gkd.controller.regular;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gkd.constant.MessageConstant;
import com.gkd.constant.RoleConstant;
import com.gkd.exception.AccountNotFoundException;
import com.gkd.pojo.dto.UserDTO;
import com.gkd.pojo.dto.UserLoginDTO;
import com.gkd.pojo.dto.UserPageQueryDTO;
import com.gkd.pojo.entity.User;
import com.gkd.pojo.vo.UserLoginVO;
import com.gkd.pojo.vo.UserVO;
import com.gkd.properties.JwtProperties;
import com.gkd.result.PageResult;
import com.gkd.result.Result;
import com.gkd.service.UserService;
import com.gkd.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 普通用户相关接口
 */
@RestController("regularUserController")
@CrossOrigin
@Slf4j
@RequestMapping("/regular/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 创建新用户
     * @param userDTO
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody UserDTO userDTO){
        log.info("用户添加:{}",userDTO);
        userDTO.setRole(RoleConstant.REGULAR);
        userService.add(userDTO);
        return Result.success();
    }

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("用户登录：{}",userLoginDTO);
        User user =userService.login(userLoginDTO);
            //登录成功后，生成jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("userId",user.getUserId());
            String token = JwtUtil.createJWT(
                    jwtProperties.getRegularSecretKey(),
                    jwtProperties.getRegularTtl(),
                    claims);

            UserLoginVO userLoginVO = UserLoginVO.builder()
                    .userId(user.getUserId())
                    .username(user.getUsername())
                    .name(user.getName())
                    .role(user.getRole())
                    .token(token)
                    .build();

            return Result.success(userLoginVO);

    }

    /**
     * 通过用户id获取信息
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    public Result<UserVO> getById(@PathVariable Integer userId){
        log.info("根据id获取用户信息:{}",userId);

        UserVO userVO = new UserVO();
        User user = userService.getById(userId);
        BeanUtils.copyProperties(user,userVO);

        return Result.success(userVO);
    }

    /**
     * 更新用户信息
     * @param userDTO
     * @return
     */
    @PutMapping
    public Result updateById(@RequestBody UserDTO userDTO){
        log.info("更新用户信息:{}",userDTO);

        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        if (StringUtils.isNotBlank(user.getPassword())){
            user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        }
        userService.updateById(user);

        return Result.success();
    }

    /**
     * 验证token
     */
    @GetMapping("/verifyAuthentication")
    public Result verifyAuthentication(){
        return Result.success();
    }
}
